//index.js
//获取应用实例
const app = getApp();
const contentService = require('../../service/content.js');
const searchService = require('../../service/search.js');

Page({
  data: {
    title:"搜索",
    currentTab: 0,
    queryParams: {
      page: 1,
      num: 20,
      keyword:'',
      city:'深圳'
    },
    contentInfo: {
      keyWord: [],
      userList:[]
    },
    resultInfo:{
      userList:[]
    },
    search:0,
    dynamicList: [],
    inputValue: null,
    isShowBackTop: false,
    isLast: false
  },
  onLoad: function () {
    this.getSearchIndex();
  },
  //滑动切换
  swiperTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
  },
  //点击切换
  clickTab: function (e) {
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      this.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  cleanSearch(){
    this.setData({
      'inputValue': ''
    })
  },

  //搜索首页
  getSearchIndex(){
    let d = {
      ...this.data.queryParams
    };
    searchService.getSearchIndex(d).then(res => {
      if (res.resCode == 0) {
        this.setData({
          contentInfo: res.object
        })
      }
    });
  },


  clickSearch: function (event) {
    var $this = this;
    //console.log(event.currentTarget.dataset.gid)
    this.setData({
      search: 1
    })
    //调用接口查询数据
    let city = wx.getStorageSync("DT-CITY");
    if (city != "" && this.data.city != city) {
      this.setData({
        'queryParams.city': city
      });
    }
    this.setData({
      'queryParams.keyword': event.currentTarget.dataset.gid
    });
    let d = {
      ...this.data.queryParams
    };
    searchService.getUser(d).then(res => {
      if (res.resCode == 0) {
        this.setData({
          resultInfo: res.object
        })
      }
    });

    searchService.getContent(d).then(res => {
      if (res.resCode == 0) {
        if (res.object) {
          let c = this.data.dynamicList.concat(res.object);
          this.setData({
            dynamicList: c
          })
          this.waterfallComponent = this.selectComponent("#waterfall2");
          console.log(this.waterfallComponent);
          this.waterfallComponent.start(res.object).then(res => { })
        } else {
          this.setData({
            dynamicList: []
          })
        }
      }
    });
  },


  formSubmit(e) {
    let obj = e.detail.value;
    //console.log("-----------------------------------------------------" + obj.searchContent.trim());
    if (obj.searchContent.trim() == "") {
      wx.showToast({
        title: "搜索内容不能为空",
        icon: "none"
      });
      return false;
    }else{
      this.setData({
        search: 1
      })
      //调用接口查询数据
      let city = wx.getStorageSync("DT-CITY");
      if (city != "" && this.data.city != city) {
        this.setData({
          'queryParams.city': city
        });
      }
      this.setData({
        'queryParams.keyword': obj.searchContent.trim()
      });
      let d = {
        ...this.data.queryParams
      };
      searchService.getUser(d).then(res => {
        if (res.resCode == 0) {
          this.setData({
            resultInfo: res.object
          })
        }
      });

      searchService.getContent(d).then(res => {
        if (res.resCode == 0) {
          if (res.object){
            let c = this.data.dynamicList.concat(res.object);
            this.setData({
              dynamicList: c
            })
            this.waterfallComponent = this.selectComponent("#waterfall2");
            this.waterfallComponent.start(res.object).then(res => { })
          }else{
            this.setData({
              dynamicList: []
            })
          }
        }
      });
      
    }
  }


})
